#include <QCheckBox>
#include <QHBoxLayout>
#include <QPainter>
#include <QRect>
#include <QMouseEvent>
#include <QShortcutEvent>
#include "ItemShortcut.h"

QPair<bool, QKeySequence> ItemShortcut::get()
{
    return qMakePair(m_isEnabled, m_keys);
}

void ItemShortcut::set(bool enable, const QKeySequence &value)
{
    m_isEnabled = enable;
    m_keys = value;
    emit itemEdited();
}

QWidget *ItemShortcut::createWidget(WidgetStyle s)
{
    auto w = new QWidget;
    QHBoxLayout *layout = new QHBoxLayout(w);
    auto checkBox = new QCheckBox;
    auto shortcutWidget = new ShortcutWidget;
    checkBox->setText(tr("Enable"));
    layout->addWidget(checkBox);
    layout->addWidget(shortcutWidget);
    w->setLayout(layout);
    connect(checkBox, &QCheckBox::clicked, this, &ItemShortcut::onWidgetEditedWrapper);
    connect(shortcutWidget, &ShortcutWidget::shortcutChanged, this, &ItemShortcut::onWidgetEditedWrapper);
    // qInfo() << checkBox->parent();
    return w;
}

void ItemShortcut::update(QWidget *widget, UpdateOption option)
{
    if (widget != nullptr)
    {
        auto checkBox = widget->findChild<QCheckBox*>();
        auto shortcutWidget = widget->findChild<ShortcutWidget*>();
        if (option == UpdateOption::WidgetContent)
        {
            checkBox->setChecked(m_isEnabled);
            shortcutWidget->set(m_keys, false); // 不要发出信号，防止死循环
            qDebug() << " UpdateOption::WidgetContent"<< m_keys;
        }
        else if (option == UpdateOption::Item)
        {
            m_isEnabled = checkBox->isChecked();
            m_keys = shortcutWidget->get();
            qDebug() << " UpdateOption::Item"<< m_keys;
        }
    }
    else
    {
        qWarning() << "";
    }
}

void ItemShortcut::writeToData(QDataStream &stream) const
{
    stream << m_isEnabled;
    stream << m_keys;
    qDebug() << "write" << m_isEnabled << m_keys;

}

void ItemShortcut::readFromData(QDataStream &stream)
{
    stream >> m_isEnabled;
    stream >> m_keys;
    qDebug() << "read" << m_isEnabled << m_keys;
    emit itemEdited();
}

ShortcutWidget::ShortcutWidget(QWidget *parent) : QWidget(parent)
{
    pressing = false;
    setFocusPolicy(Qt::FocusPolicy::StrongFocus);
    setMinimumSize(150, 25);
}

void ShortcutWidget::set(const QKeySequence &value, bool isEmit)
{
    m_keys = value;
    update();
    if (isEmit)
        emit shortcutChanged(value);
}

QKeySequence ShortcutWidget::get()
{
    return m_keys;
}

void ShortcutWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    if (pressing)
    {
        painter.setPen(Qt::NoPen);
        painter.fillRect(this->rect(), Qt::red);
    }
    else
    {
        painter.setPen(QPen(QColor(100, 100, 100), 2));
        painter.drawRect(QRect(this->rect().topLeft() + QPoint(1, 1), this->rect().bottomRight() - QPoint(1, 1)));
        if(m_keys.isEmpty())
        {
            painter.drawText(this->rect(), Qt::AlignCenter, tr("Press Shortcut"));
        }
        else
        {
            painter.drawText(this->rect(), Qt::AlignCenter, m_keys.toString(QKeySequence::PortableText));
        }
    }
}

bool ShortcutWidget::event(QEvent *event)
{

    // static QKeyEvent keyRecorder(QEvent::KeyPress, 0, 0);
    static Qt::KeyboardModifiers kMRecorder = 0;
    static int kCRecorder = 0;
    QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
    if (mouseEvent)
    {
        mouseEvent->accept();
        return true;
    }
    else if (event->type() == QEvent::KeyPress)
    {
        pressing = true;
        QKeyEvent *ke = dynamic_cast<QKeyEvent*>(event);
        switch (ke->key())
        {
        case Qt::Key::Key_Control:
            kMRecorder.setFlag(Qt::KeyboardModifier::ControlModifier);
            break;
        case Qt::Key::Key_Alt:
            kMRecorder.setFlag(Qt::KeyboardModifier::AltModifier);
            break;
        case Qt::Key::Key_Shift:
            kMRecorder.setFlag(Qt::KeyboardModifier::ShiftModifier);
            break;
        case Qt::Key::Key_Meta:
            kMRecorder.setFlag(Qt::KeyboardModifier::MetaModifier);
            break;
        default:
            kCRecorder = ke->key();
        }
        update();
        return true;
    }
    else if (event->type() == QEvent::KeyRelease)
    {
        if (pressing)
        {
            pressing = false;
            m_keys = kMRecorder + kCRecorder;
            kMRecorder = 0;
            kCRecorder = 0;
            qInfo() << m_keys.toString(QKeySequence::NativeText);
            update();
            emit shortcutChanged(m_keys);
        }
        return true;
    }

    return QWidget::event(event);

}




